import { SIGN_IN } from 'api/user';
import { ERR_CODE } from 'config/config.default';
import { http as axios } from 'plugins/request/index';
import { SIGN_IN_CLIENTS, SIGN_IN_TYPES } from 'config/config.user';

/**
 * 保存token
 * @param {string} token 服务端返回的token数据
 */
export const setToken = token => {
  uni.setStorageSync('token', token);
};

/**
 * 获取token
 * @return {string} 本地保存的token
 */
export const getToken = () => uni.getStorageSync('token');

/**
 * 提交登录信息
 * @param {object} params 提交的参数
 */
export const signIn = params => {
  return new Promise((resolve, reject) => {
    axios
      .post(SIGN_IN, params)
      .then(res => {
        const { success, code, data, msg } = res.data;
        if (success && code === ERR_CODE) {
          if (!data || !data.token) {
            reject('服务端返回数据不正确');
          } else {
            resolve(data);
          }
        } else {
          reject(`登录请求失败 ${msg}`);
        }
      })
      .catch(err => {
        reject(err);
      });
  });
};

// 微信登录
export const wxLogin = () => {
  return new Promise((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success(response) {
        if (response.code) {
          const params = {
            client: SIGN_IN_CLIENTS['mp'],
            type: SIGN_IN_TYPES['mp'],
            data: { identifier: response.code, credential: 'health' },
            // redirect: 'https://test.tall.wiki/gateway/health/initMsg',
            redirect: 'https://www.tall.wiki/gateway/health/initMsg',
          };
          resolve(params);
        } else {
          reject(response.errMsg);
        }
      },
      fail() {
        console.log('fail');
        reject('微信登录失败');
      },
    });
  });
};

// 企业微信登录
export const wxWorkLogin = () => {
  return new Promise((resolve, reject) => {
    wx.qy.login({
      provider: 'weixin',
      success(response) {
        if (response.code) {
          const params = {
            client: SIGN_IN_CLIENTS['wx_work'],
            type: SIGN_IN_TYPES['wx_work'],
            data: { identifier: response.code, credential: 'health' },
            // redirect: 'https://test.tall.wiki/gateway/health/initMsg',
            redirect: 'https://www.tall.wiki/gateway/health/initMsg',
          };
          resolve(params);
        } else {
          reject(response.errMsg);
        }
      },
      fail() {
        console.log('fail');
        reject('微信登录失败');
      },
    });
  });
};

// 小程序登录
export const mpLogin = () => {
  try {
    const res = uni.getSystemInfoSync();
    if (res.environment === 'wxwork') {
      return wxWorkLogin();
    } else {
      return wxLogin();
    }
  } catch (err) {
    console.log('err: ', err);
  }
};

// 钉钉登录
export const ddLogin = () => {
  console.log('dingtalk login.........');
  try {
    dd.getAuthCode({
      success(res) {
        console.log('res: ', res);
        /*{
            authCode: 'hYLK98jkf0m' // string authCode
        }*/
      },
      fail: function(err) {
        console.log('err: ', err);
      },
    });
  } catch (error) {
    console.log('error: ', error);
  }
};
